package com.citywithincity.lock.impl;

import java.util.concurrent.locks.ReentrantLock;

import com.citywithincity.lock.Lock;
import com.citywithincity.lock.LockFactory;

public class ReentrantLockFactory implements LockFactory {
	
	private ReentrantLock[] locks;
	public ReentrantLockFactory(){
		this(20);
	}
	public ReentrantLockFactory(int count) {
		locks = new ReentrantLock[count];
		for(int i=0; i< count; ++i){
			locks[i] = new ReentrantLock();
		}
	}

	@Override
	public Lock getLock(String name) {
		int index = name.hashCode()%locks.length;
		index = Math.abs(index);
		return new LockImpl ( locks[index] );
	}
	
	
}
